from lxml import etree
import requests

def getContent(url,params):
    try:
        agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
        session=requests.session()
        response=session.get(url=url,params=params,headers={"User-Agent":agent})
        response.raise_for_status() #如果请求过程发送错误则返回一个HttpError对象
        response.encoding=response.apparent_encoding #查查什么意思
    except Exception as e:
        print("爬取错误")
    else:
        response.close()
        return response.content.decode("utf-8")

def parserContent(htmlContent):
    #finish 完成
    #authentication 认证
    tree=etree.HTML(htmlGontent)
    divList=tree.xpath('/html/body/div[6]/div/div/div[3]/div[5]/div[1]/div')

    num=0
    dispose=[]
    for i in divList:
        companyName = i.xpath('./div/div/a[1]/div[1]/p/text()')[1].replace('\n','')
        explain="sass".join(i.xpath('./div/div/a[2]/div[2]/div[2]/p/text()'))
        price=i.xpath('./div/div/a[2]/div[2]/div[1]/span[1]/text()')[0].strip("¥")
        bargin=i.xpath('./div/div/a[2]/div[2]/div[1]/span[2]/text()')
        finish=i.xpath('./div/div/a[2]/div[2]/div[3]/span[1]/text()')
        location=i.xpath('./div/div/a[1]/div[1]/div[1]/span[1]/text()')
        grade=i.xpath('./div/div/a[1]/div[2]/span[2]/i[2]/text()')
        authentication=i.xpath('./div/div/a[1]/div[2]/span[3]/i[1]/text()')


if __name__ == '__main__':
    params={
        "wk" : "java"
    }
    url="https://chengdu.zbj.com/search/f/?kw=java"
    htmlGontent=getContent(url,params)
    parserContent(htmlGontent)